#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "arkime.h"
#include "sendSession.h"

void sendSessionAndPacket(const char*id,const char *sessionId, GArray *packetsArray,const char *server_ip,int server_port) {
    int sock;
    struct sockaddr_in server_addr;
    unsigned char separator_session[8] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED, 0xFA, 0xCE};
    unsigned char separator_pcap[8] = {0xBA, 0xAD, 0xC0, 0xFF, 0xEE, 0x12, 0x34, 0x56};
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        perror("Socket creation failed");
        return;
    }
    // 设置服务器地址
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(server_port);
    inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
    // 连接服务器
    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        perror("Connection failed");
        close(sock);
        return;
    }
    // 复制 sessionId 的完整内容
    size_t json_size = strlen(sessionId);
    char *json_data = (char *)malloc(json_size + 1);  // +1 预留 '\0'
    if (json_data == NULL) {
        perror("Memory allocation failed");
        close(sock);
        return;
    }
    snprintf(json_data, json_size + 1, "%s", sessionId);
    // 计算总负载大小
    size_t total_size = json_size + sizeof(separator_session); // sessionId + separator_session
    for (guint i = 0; i < packetsArray->len; i++) {
        ArkimePacket_t *packet = g_array_index(packetsArray, ArkimePacket_t *, i);
        total_size += sizeof(packet->ts.tv_sec) + sizeof(packet->ts.tv_usec) + sizeof(packet->pktlen) + packet->pktlen + sizeof(separator_pcap) + sizeof(separator_session);
    }
    // 构建 HTTP POST 请求头
    size_t header_size = total_size + 128;
    char *request_header = (char *)malloc(header_size);
    if (request_header == NULL) {
        perror("Memory allocation failed");
        free(json_data);
        close(sock);
        return;
    }
    snprintf(request_header, header_size,
             "POST /realtime/handle_realtime_data?id=%s HTTP/1.1\r\n"
             "Host: %s:%d\r\n"
             "Content-Type: application/octet-stream\r\n"
             "Content-Length: %zu\r\n"
             "\r\n",id,
             server_ip, server_port, total_size);
    // 发送请求头
    if (send(sock, request_header, strlen(request_header), 0) < 0) {
        perror("Failed to send request header");
        free(json_data);
        free(request_header);
        close(sock);
        return;
    }
    // 发送 sessionId 和 separator_session
    if (send(sock, json_data, json_size, 0) < 0) {
        perror("Failed to send sessionId");
        free(json_data);
        free(request_header);
        close(sock);
        return;
    }
    if (send(sock, separator_session, sizeof(separator_session), 0) < 0) {
        perror("Failed to send session separator");
        free(json_data);
        free(request_header);
        close(sock);
        return;
    }
    // 发送每个 packet 数据和 separator_session, separator_pcap
    for (guint i = 0; i < packetsArray->len; i++) {
        ArkimePacket_t *packet = g_array_index(packetsArray, ArkimePacket_t *, i);

        // 每个 packet 之前用 separator_session 分隔
        if (i > 0) {
            if (send(sock, separator_session, sizeof(separator_session), 0) < 0) {
                perror("Failed to send packet separator");
                free(json_data);
                free(request_header);
                close(sock);
                return;
            }
        }
        // 发送 packet 内的各个字段，使用 separator_pcap 分隔
        if (send(sock, &packet->ts.tv_sec, sizeof(packet->ts.tv_sec), 0) < 0 ||
            send(sock, separator_pcap, sizeof(separator_pcap), 0) < 0 ||
            send(sock, &packet->ts.tv_usec, sizeof(packet->ts.tv_usec), 0) < 0 ||
            send(sock, separator_pcap, sizeof(separator_pcap), 0) < 0 ||
            send(sock, &packet->pktlen, sizeof(packet->pktlen), 0) < 0 ||
            send(sock, separator_pcap, sizeof(separator_pcap), 0) < 0 ||
            send(sock, packet->pkt, packet->pktlen, 0) < 0) {
            perror("Failed to send packet data");
            free(json_data);
            free(request_header);
            close(sock);
            return;
        }
    }
    // 清理并关闭
    free(json_data);
    free(request_header);
    close(sock);
}